/**
 * Copyright  2025, Hai Yue Xing He Technology Co., Ltd
 * 
 * @author        ZHAO LIMIN
 * @version       1.0.1
 * @since         2025-04
 * cion.ts        金币，根据Line 的信息进行动态加载
 *                作为一个独立的组件，用于显示金币
 */
import { _decorator, Component, EPSILON, Node, resources, Sprite, SpriteFrame } from 'cc';
import { ICartesian } from '../hyxh/core/Cartesian';
import { HNetsPolyline } from '../hyxh/core/HYXHNetsPolyline';
import { defined } from '../hyxh/core/Defined';
import { Check } from '../hyxh/core/Check';
import { SecenseTools } from './SecenseTools';
import { Cartesian3 } from '../hyxh/core/Cartesian3';
import { HYXHMathUtil } from '../hyxh/core/HYXHMathUtil';
import { SecenesManager } from './SecenesManager';
import { HImageResource } from './HResource';
const { ccclass, property } = _decorator;

@ccclass('Cion')
export class Cion extends Component {
    position : ICartesian = {x: 0, y: 0, z: 0}; 
    thisnode : Node | null = null;
    spriteFrame: SpriteFrame | null = null;
    network: HNetsPolyline | null = null;
    width: number = 0;
    height: number = 0;
    rid: number = 0;

    /* 表示有金币, active 为false, 没有显示了 */
    public activity: boolean = false;

    public forEated : boolean = false;

    /* 是否显示 */
    public show: boolean = true;

    /* timeout, 在吃豆被吃后，进行计时间，归零后，复活 */
    private _timeout: number = 0;
    
    /**
     * 记录相同位置的点，用来进行动态加载
     * 避免，同一个点，多次加载
     */
    points : Array<number> = [];

    /* 记录当前cion 是否为门 */
    _stage_sign : boolean = false;
    /* 记录当前cion 是否为电梯 路径点 */
    _elevator_sign : boolean = false;

    sprite : Sprite = null;

    public hasExistPoints(path: number, index: number) : boolean{
        if (!defined(this.points)) {
            return false; 
        }   
        for (let i = 0; i < this.points.length; i += 2) {
            if ((this.points[i] == path) && (this.points[i+1] == index)){
                return true; 
            }  
        }
        return false;
    }

    public hasSamePath(path: number) {
        if (!defined(this.points)) {
            return false; 
        }   
        for (let i = 0; i < this.points.length; i += 2) {
            if (this.points[i] == path){
                return true; 
            }  
        }
        return false;
    }

    public setStageSign(stageSign: boolean) {
        this._stage_sign = stageSign;
    }

    public getStageSign(): boolean {
        return this._stage_sign;
    }

    public setElevatorSign(elevatorSign: boolean) {
        this._elevator_sign = elevatorSign;
    }

    public getElevatorSign(): boolean {
        return this._elevator_sign;
    }
    public initME(
        _node_name : string,
        _position: ICartesian,         
        _network: HNetsPolyline,
        _rid: number,
        _show: boolean = true) {
        this.name = _node_name;
        this.position = _position??null;   
        this.network = _network??null;
        this.activity = false;
        this.show = _show;
        this.rid = _rid;
        this.thisnode = new Node(this.name);
        this.thisnode.setParent(this.node); 
    }
    public showME() {
        this.show = true;        
        this.activity = true;
        this.forEated = false;
        const image_resource = SecenesManager.getInstance().getResource(this.rid) as HImageResource;
        if (!defined(image_resource)) {
            return;
        }
    
        if (!defined(image_resource.spriteFrame)) {
            return;
        } 
        SecenesManager.showImage(this.thisnode, image_resource);
        this.thisnode.setPosition(this.position.x, this.position.y, this.position.z); // 设置节点的位置
    }

    public hideME() {
        this.show = false;
        //this.activity = false;        
        SecenesManager.hideImage(this.thisnode);
    }

    public hasSamePosition(layer: number, posIndex: number, pushable: boolean = false): boolean {
        Check.defined("layer", layer);
        Check.defined("posIndex", posIndex);
        Check.typeOf.number("layer", layer);
        Check.typeOf.number("posIndex", posIndex);
        if (!defined(this.network)) {
            return false;
        }
        let _same = false;       
        for (let i = 0; i < this.points.length; i += 2) {
            if ((this.points[i] == layer) && (this.points[i+1] == posIndex)){
                _same = true;
                return true;
            }
            if (this.network.equalsPosition(this.points[i], this.points[i+1], layer, posIndex)) {
                _same = true;
                break;
            }
        }
        if (_same && pushable) {
            this.points.push(layer);
            this.points.push(posIndex);
        }
        return _same;
    }
    /**
     * 判断路径是否满足
     * @param layer 
     * @param startIndex 
     * @param endIndex 
     * @returns 
     */
    public forInRange(layer: number, startIndex:number, endIndex:number): boolean {
        if (!defined(this.network)) {
            return false;
        }

        if (!defined(this.points)) {
            return false;
        }

        if (this.points.length <= 0) {
            return false;
        }

        const length = this.network.getPathSize(layer);
        if (!defined(length)){
            return false;
        }
        if (length <= 0) {
            return false;
        }
        let _start = startIndex;
        if (_start < 0 ) {
            _start = length + _start;
        }

        let _end = endIndex;
        if (_end < 0 ) {
            _end = length + _end;
        }

        let _cursor_index = 0;
        while (_cursor_index < this.points.length) {
            const _layer_cion = this.points[_cursor_index++];
            const _index_cion = this.points[_cursor_index++];
            if (_layer_cion != layer) continue;
            if ((_index_cion >= Math.min(_start, _end)) &&
                (_index_cion <= Math.max(_start, _end)) ) {
                return true;
            }
        }
        return false;
    } 


    public eated() {
        this.show = false;
        this.forEated = true;
        /* 超时等待 */
        this._timeout = SecenseTools.MAX_IDLE_TIME_CION;
    }

    public releaseSprite() {
        if (defined(this.thisnode)) {
            this.hideME();
            this.activity = false;
            this.eated();
        }
    }

    public relife() { 
        this.show = true;
        this.forEated = false;
        /* 超时等待 */
        this._timeout = 0;
        this.showME();
    }
    public updateTimeStamp(_time_stamp: number) {
        if (this.forEated) {
            if (_time_stamp > 0) {
                this._timeout -= _time_stamp;
            }
        }
    }

    public isCionEated() {
        return this.forEated;
    }

    public isCanRelife() : boolean {
        return (this.forEated) && (this._timeout <= 0);
    }

    public firstPoint() : Array<number> {
        if(!defined(this.points)) {
            return [];
        }
        if ( this.points.length < 2) {
            return [];
        }
        return [this.points[0], this.points[1]]
    }

    public equalsPosition(next_node_pos:Cion): boolean {
        if (!defined(this.position)) {
            return false; 
        }
        if (!defined(next_node_pos)) {
            return false; 
        }

        const pos_1 = new Cartesian3(this.position.x, this.position.y, this.position.z);
        const pos_2 = new Cartesian3(next_node_pos.position.x, next_node_pos.position.y, next_node_pos.position.z);
        return Cartesian3.equalsEpsilon(pos_1, pos_2, HYXHMathUtil.EPSILON2); 
    }
}


